package m.jclecai.com.dialog;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.inter.OnResultListener;
import m.jclecai.com.view.FlowLayout;

public class BiFenChooseGameDialog extends BaseDialog {

    @BindView(R.id.dialog_fl_game_type)
    FlowLayout dialog_fl_game_type;
    String games[] ;
    private OnResultListener onResultListener;

    public BiFenChooseGameDialog(Context context) {
        super(context);
    }

    private void initView(Context context) {
        setContentView(R.layout.dialog_choose_game);
        initGameType(context);
    }

    private void initGameType(final Context context) {
        dialog_fl_game_type.removeAllViews();
        for (String game : games) {
            TextView textView = new TextView(context);
            textView.setPadding(24, 12, 24, 12);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(12, 12, 12, 12);
            setSelect(false, textView);
            textView.setText(game);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setSelect(!(view).isSelected(), (TextView) view);

                }
            });
            dialog_fl_game_type.addView(textView, layoutParams);
        }
    }

    private void setSelect(boolean b, TextView textView) {
        if (b) {
            textView.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.bg_red_rectangle_corner_border_red));
            textView.setTextColor(getContext().getResources().getColor(R.color.white));
        } else {
            textView.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.bg_white_rectangle_corner));
            textView.setTextColor(getContext().getResources().getColor(R.color.black));
        }
        textView.setSelected(b);
    }

    public BiFenChooseGameDialog(Context context, boolean fromButton,String[] games) {
        super(context, fromButton);
        this.games=games;
        initView(context);
    }

    @OnClick(R.id.dialog_btn_game_type_sure)
    public void sure() {
        //获取选中的数据
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < dialog_fl_game_type.getChildCount(); i++) {
            TextView textView = ((TextView) dialog_fl_game_type.getChildAt(i));
            if (textView.isSelected()) {
                stringBuffer.append(textView.getText().toString());
            }
        }
        //处理回调数据
        if (onResultListener != null) {
            onResultListener.onReturnResult(stringBuffer.toString());
        }
        dismiss();
    }

    @OnClick(R.id.dialog_btn_game_type_cancel)
    public void cancel() {
        dismiss();
    }

    public void setCallBack(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }
}
